{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先安装模块 pip install pypiwin32\n",
    "import win32api, win32con"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 打开注册表\n",
    "\n",
    "对打开的子项进行的操作，包括win32con.KEY_ALL_ACCESS、win32con.KEY_READ、win32con.KEY_WRITE等\n",
    "\n",
    "获得句柄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<PyHKEY:1216>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,\n",
    "                          'SOFTWARE\\Microsoft\\Internet Explorer', 0,\n",
    "                          win32con.KEY_READ)\n",
    "key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'PyHANDLE'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key.__class__.__name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1216"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key.handle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "key.close()\n",
    "# 或者\n",
    "# win32api.RegCloseKey(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "import winreg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key.handle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,\n",
    "                          'SOFTWARE\\Microsoft\\Internet Explorer', 0,\n",
    "                          win32con.KEY_READ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 读取默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegQueryValue(key, '')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 读取某一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('9.11.17134.0', 1)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegQueryValueEx(key, 'Version')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 查询项的基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(33, 10, 131916049733905679)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegQueryInfoKey(key)    # 返回项的子项数目、项值数目，以及最后一次修改时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Class': '',\n",
       " 'LastWriteTime': pywintypes.datetime(2019, 1, 10, 14, 42, 53, 390000, tzinfo=TimeZoneInfo('GMT Standard Time', True)),\n",
       " 'MaxClassLen': 0,\n",
       " 'MaxSubKeyLen': 25,\n",
       " 'MaxValueLen': 78,\n",
       " 'MaxValueNameLen': 17,\n",
       " 'Reserved': None,\n",
       " 'SubKeys': 33,\n",
       " 'Values': 10,\n",
       " 'cbSecurityDescriptor': 368}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegQueryInfoKeyW(key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设置值\n",
    "RegSetValueEx(key，valueName，reserved，type，value) 要修改或重新设置注册表**某一项**的项值。如果项值存在，则修改该项值，如果不存在，则添加该项值。\n",
    "\n",
    "RegSetValue(key，subKey，type，value) 设置项的  ** 默认值 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将“HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer”\n",
    "# 的默认值设为python\n",
    "win32api.RegSetValue(key, '', win32con.REG_SZ, 'python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将其“Version”设置为7.0.2900.2180\n",
    "win32api.RegSetValueEx(key, 'Version', 0, win32con.REG_SZ, '7.0.2900.2180')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 添加、删除项\n",
    "\n",
    "RegCreateKey（key，subKey） 向注册表中添加项， 返回句柄\n",
    "\n",
    "RegDeleteKey（key，subKey） 删除注册表中的项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 向“HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer”添加子项“Python”\n",
    "win32api.RegCreateKey(key, 'Python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 新创建的子项的句柄\n",
    "# 删除刚才创建的子项“Python”\n",
    "win32api.RegDeleteKey(key, 'Python')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 枚举 键、值（通过下标）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打开Software目录，默认把注册表建在此处\n",
    "key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\n",
    "                          'Software', 0, win32con.KEY_ALL_ACCESS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Adobe'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegEnumKey(key, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(('2345.com',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 8, 26, 12, 26, 0, 643000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Adobe',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 21, 13, 28, 34, 679000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Apowersoft',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 7, 16, 1, 5, 51, 482000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('AppDataLow',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 12, 18, 10, 0, 14, 802000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('ASUS',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 948000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Baidu',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 964000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Bmupd',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 964000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('BugSplat',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 12, 18, 14, 42, 16, 995000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Bytescout',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 7, 16, 1, 5, 54, 76000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Catch22',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 964000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Chromium',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 19, 8, 32, 40, 512000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Clients',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 964000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('CoreAAC',\n",
       "  0,\n",
       "  'REG_SZ',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 979000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('CyberLink',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 979000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('DSP-worx',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 5, 1, 44, 377000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('DuoDianApp',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 7, 23, 4, 35, 46, 840000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('duowan',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 979000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('ej-technologies',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 35, 979000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('f8c0fbbe-5e62-535c-b687-54387a5b3b06',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 36, 136000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Gabest',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 20, 1, 37, 43, 532000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Google',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 36, 214000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('HaoZip',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 7, 17, 2, 43, 39, 104000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('IM Providers',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 36, 261000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Intel',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 36, 261000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('JavaSoft',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 36, 261000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('JetBrains',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 9, 3, 1, 44, 44, 47000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('kdesk',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 18, 4, 14, 13, 571000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Kingsoft',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 5, 2, 3, 16, 146000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('KLive',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 7, 1, 43, 51, 225000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Logitech',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 8, 21, 12, 50, 13, 967000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Martin Prikryl',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 36, 276000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Microsoft',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 15, 8, 50, 354000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('MiKTeX.org',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 4, 13, 53, 2, 769000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Mozilla',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 11, 15, 14, 28, 55, 626000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('MozillaPlugins',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 13, 9, 30, 0, 539000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('NetSarang',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 651000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Netscape',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 651000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('NVIDIA Corporation',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 1, 4, 52, 3, 396000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('ODBC',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 667000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('OpenCV',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 698000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Oracle',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 1, 11, 14, 45, 572000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Policies',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 34, 13, 282000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('PremiumSoft',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 20, 9, 7, 6, 622000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Python',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 21, 16, 22, 19, 580000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('QQBrowser',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 21, 11, 59, 34, 167000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('QQVipDownloader',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 17, 4, 48, 44, 514000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('QtProject',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 7, 17, 1, 12, 23, 408000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('RegisteredApplications',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 3, 49, 24, 867000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('roamingdevice',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 12, 29, 14, 41, 35, 751000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Sigma-rt, Inc.',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 698000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('SogouExplorer',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 18, 7, 27, 29, 181000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('SogouInput',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 16, 26, 20, 571000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('SogouInput.ppup',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 713000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('SyncEngines',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 39, 713000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Tencent',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 4, 50, 47, 100000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Thunder Network',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 18, 11, 35, 34, 666000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('TPHelper',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 8, 21, 12, 28, 54, 508000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Trolltech',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 40, 57000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('TUG',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 4, 14, 27, 47, 945000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('VMware, Inc.',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 7, 23, 4, 16, 25, 716000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Wow6432Node',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 40, 57000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('XK72 Ltd',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 12, 22, 2, 43, 52, 898000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Yinxiang Biji',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 1, 21, 12, 27, 21, 658000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('{6A896522-C181-40E4-A1C6-CCE7795E10D3}',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2018, 6, 4, 0, 35, 40, 57000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('Classes',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 1, 3, 42, 23, 835000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('SogouInput.ppup.user',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 3, 56, 40, 25000, tzinfo=TimeZoneInfo('GMT Standard Time', True))),\n",
       " ('SogouInput.user',\n",
       "  0,\n",
       "  '',\n",
       "  pywintypes.datetime(2019, 2, 2, 16, 26, 10, 515000, tzinfo=TimeZoneInfo('GMT Standard Time', True))))"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegEnumKeyEx(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegEnumKeyExW(key) == win32api.RegEnumKeyEx(key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Path',\n",
       " 'F:\\\\python37\\\\Scripts\\\\;F:\\\\python37\\\\;C:\\\\Users\\\\wanzheng\\\\AppData\\\\Local\\\\Microsoft\\\\WindowsApps;C:\\\\Users\\\\wanzheng\\\\AppData\\\\Local\\\\GitHubDesktop\\\\bin;C:\\\\Users\\\\wanzheng\\\\AppData\\\\Local\\\\Microsoft\\\\WindowsApps;',\n",
       " 1)"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 环境变量\n",
    "key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\n",
    "                          'Environment', 0, win32con.KEY_ALL_ACCESS)\n",
    "\n",
    "win32api.RegEnumValue(key ,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 例1\n",
    "\n",
    "检测注册表某一项下HKEY_CURRENT_USER\\Software是否存在某个子项，存在则读取其值，不存在则建立子项并且赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "67"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import win32api\n",
    "import win32con\n",
    "\n",
    "# 打开Software目录，默认把注册表建在此处\n",
    "key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\n",
    "                          'Software', 0, win32con.KEY_ALL_ACCESS)\n",
    "\n",
    "# 查询key下的子项个数\n",
    "size = win32api.RegQueryInfoKey(key)[0]\n",
    "size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Class': '',\n",
       " 'LastWriteTime': pywintypes.datetime(2019, 2, 2, 5, 1, 44, 377000, tzinfo=TimeZoneInfo('GMT Standard Time', True)),\n",
       " 'MaxClassLen': 6,\n",
       " 'MaxSubKeyLen': 38,\n",
       " 'MaxValueLen': 0,\n",
       " 'MaxValueNameLen': 0,\n",
       " 'Reserved': None,\n",
       " 'SubKeys': 67,\n",
       " 'Values': 0,\n",
       " 'cbSecurityDescriptor': 232}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win32api.RegQueryInfoKeyW(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 遍历子项，查看是否存在已经添加的项，例如我们添加的是test\n",
    "result = False\n",
    "\n",
    "for i in range(size):\n",
    "    # 若不进行try操作，如果test不存在的话会有异常\n",
    "    try:\n",
    "        if win32api.RegEnumKey(key, i) != 'test':\n",
    "            continue\n",
    "        else:\n",
    "            result = True\n",
    "            break\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        continue\n",
    "# 如果存在读出数据\n",
    "if(result):\n",
    "    # 打开test目录，取出其默认值\n",
    "    keywx = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\n",
    "                                'Software\\test', 0, win32con.KEY_ALL_ACCESS)\n",
    "    info = win32api.RegQueryValue(keywx, '')\n",
    "    print(info)\n",
    "    # 关闭注册表\n",
    "    win32api.RegCloseKey(keywx)\n",
    "\n",
    "else:\n",
    "    # 新建test子项\n",
    "    win32api.RegCreateKey(key, 'test')\n",
    "    # 给其赋值\n",
    "    win32api.RegSetValue(key, 'test', win32con.REG_SZ, 'ttttt')\n",
    "    # 关闭注册表\n",
    "    win32api.RegCloseKey(key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 例2\n",
    "对注册表进行修改的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import win32api, win32con\n",
    "import sys\n",
    "\n",
    "\n",
    "def ModifyReg(key, keyPath, valueName, valueType, value):\n",
    "    try:\n",
    "        '''\n",
    "        RegConnectRegistry: \n",
    "            computerName: string(If None, the local computer is used)\n",
    "            key: int(May be win32con.HKEY_LOCAL_MACHINE...)\n",
    "        '''\n",
    "        keyHandle = win32api.RegConnectRegistry(None, key)\n",
    "        '''\n",
    "        RegOpenKeyEx:\n",
    "            key: PyHKEY/int\n",
    "            subKey: string\n",
    "            reserved = 0: int(Reserved. Must be zero.)\n",
    "            sam = KEY_READ: int(If you want to set the value later, you must open the key with KEY_SET_VALUE)\n",
    "        '''\n",
    "        subkeyHandle = win32api.RegOpenKeyEx(keyHandle, keyPath)\n",
    "        '''\n",
    "        RegQueryValueEx:\n",
    "            key: PyHKEY/int\n",
    "            valueName: The name of the value to query\n",
    "        '''\n",
    "        (currValue, type) = win32api.RegQueryValueEx(subkeyHandle, valueName)\n",
    "        if (currValue == value):\n",
    "            print('PASS: Check reg value: %s' % valueName)\n",
    "            return 1\n",
    "        else:\n",
    "            print('INFO: The %s is not the same as %s' % (valueName, value))\n",
    "            print('INFO: Try to set %s as %s' % (valueName, value))\n",
    "            subkeyHandle = win32api.RegOpenKeyEx(keyHandle, keyPath, 0,\n",
    "                                                 win32con.KEY_SET_VALUE)\n",
    "            '''\n",
    "            RegSetValueEx:\n",
    "                key: PyHKEY/int\n",
    "                valueName: string(The name of the value to set)\n",
    "                reserved: any(Zero will always be passed to the API function)\n",
    "                type: int(REG_DWORD, REG_SZ ...)\n",
    "                value: registry data\n",
    "            '''\n",
    "            win32api.RegSetValueEx(subkeyHandle, valueName, 0, valueType,\n",
    "                                   value)\n",
    "    except:\n",
    "        print(\n",
    "            'FAIL: ModifyReg %s. Exception happened. Exception happened when accessing registry key under %s.'\n",
    "            % (valueName, keyPath))\n",
    "        return 0\n",
    "    print('SUCCESS: ModifyReg %s value under %s' % (valueName, keyPath))\n",
    "    return 1\n",
    "\n",
    "\n",
    "def main():\n",
    "    key = win32con.HKEY_LOCAL_MACHINE\n",
    "    AUPath = r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update'\n",
    "    valueName = r'AUOptions'\n",
    "    valueType = win32con.REG_DWORD\n",
    "    value = 1\n",
    "    ModifyReg(key, AUPath, valueName, valueType, value)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    sys.exit(main())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 例3\n",
    "清理掉卸载软件后的所有残留注册表项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "import winreg\n",
    "import re\n",
    "\n",
    "\n",
    "# 获取一个注册表键的所有子健\n",
    "def SubKeys(Ob_S_key):\n",
    "    try:\n",
    "        i = 0\n",
    "        L_SubKeys = []\n",
    "        while 1:\n",
    "            Name = winreg.EnumKey(Ob_S_key, i)\n",
    "            L_SubKeys.append(Name)\n",
    "            i += 1\n",
    "    except WindowsError:\n",
    "        return L_SubKeys\n",
    "\n",
    "\n",
    "# 删除主键和主键下的所有子健\n",
    "def Del_SubKey(Ob_MasterKey, Str_SubKey):\n",
    "    try:\n",
    "        Ob_DS_Key = winreg.OpenKey(Ob_MasterKey, Str_SubKey)\n",
    "        for sbkey in SubKeys(Ob_DS_Key):\n",
    "            Str_DS_SubKey = Str_SubKey+\"\\\\\"+sbkey\n",
    "            Del_SubKey(Ob_MasterKey, Str_DS_SubKey)\n",
    "        winreg.DeleteKey(Ob_MasterKey, Str_SubKey)\n",
    "    except WindowsError:\n",
    "        pass\n",
    "\n",
    "\n",
    "# 递归获取一个主键下包含关键字的所有子健\n",
    "def SearchSubkey(Ob_MasterKey, Str_SubKey, Keyname):\n",
    "    try:\n",
    "        Ob_SS_Key = winreg.OpenKey(Ob_MasterKey, Str_SubKey)\n",
    "        for sk in SubKeys(Ob_SS_Key):\n",
    "            Str_SS_SubKey = Str_SubKey+\"\\\\\"+sk\n",
    "            if re.search(Keyname, sk, re.IGNORECASE):\n",
    "                print(\"Delete   \"+Str_SS_SubKey)\n",
    "                Del_SubKey(Ob_MasterKey, Str_SS_SubKey)\n",
    "            else:\n",
    "                SearchSubkey(Ob_MasterKey, Str_SS_SubKey, Keyname)\n",
    "    except WindowsError:\n",
    "        pass\n",
    "\n",
    "\n",
    "# 获取根键下包含关键字的所有子健\n",
    "def Search_KN_SubKey(Ob_MasterKey, Str_SubKey, Keyname):\n",
    "    Ob_SKS_Key = winreg.OpenKey(Ob_MasterKey, Str_SubKey)\n",
    "    for Str_SKS_SubKey in SubKeys(Ob_SKS_Key):\n",
    "        SearchSubkey(Ob_MasterKey, Str_SKS_SubKey, Keyname)\n",
    "        # print Str_M_SubKey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Delete   SOFTWARE\\Classes\\AppID\\{31337EC7-5767-11CF-BEAB-00AA006C3606}\n",
      "Delete   SOFTWARE\\Classes\\AppID\\{57360832-5F9B-4190-8467-000D2D510212}\n",
      "Delete   SOFTWARE\\Classes\\AppID\\{6f33340d-8a01-473a-b75f-ded88c8360ce}\n",
      "Delete   SOFTWARE\\Classes\\AppID\\{AD829705-CCA8-44D4-88E0-331E48336059}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{009F3B45-8A6B-4360-B997-B2A009A16402}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{0316BBC2-92D9-4E2E-8345-3609C6B5C167}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{07B65360-C445-11CE-AFDE-00AA006C14F4}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{222B1D13-08D6-4BE8-B0C0-948A736012D0}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{276C88CA-7533-4A86-B676-66B36080D484}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{29625281-51CE-3F8A-AC4D-E360CACB92E2}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{2BB6C5E0-C2B9-3608-8868-21CFD6DDB91E}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{31337EC7-5767-11CF-BEAB-00AA006C3606}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{3318360C-1AFC-4D09-A86B-9F9CB6DCEB9C}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{34684F0B-6721-4DC5-AF04-A8C2C1C3609E}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{37F45E7D-A630-41CD-B2D7-D360A69A3A49}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{4b360c3c-d284-4384-abcc-ef133e1445da}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{57360832-5F9B-4190-8467-000D2D510212}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{639F5AF5-BCED-4369-AC34-360B16D955FD}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{6f33340d-8a01-473a-b75f-ded88c8360ce}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{73959FD1-7360-42F7-807D-622341783DC0}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{748F920F-FB24-4D09-B360-BAF6F199AD6D}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{81CFA1FF-360D-4368-973A-670B8D2AA3B9}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{83E94DBF-7F97-46B0-A6F0-360FE982BF83}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{859AC360-A8F0-4d82-BBC7-7E28529FCC46}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{87FC0268-9A55-4360-95AA-004A1D9DE26C}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{9c7a1728-b694-427a-94a2-a1b2c60f0360}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{9DCC3CC8-8609-4863-BAD4-03601F4C65E8}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{A37B2DFB-4204-4476-B1D9-29413836093D}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{A38E6420-19EC-490c-B4BF-51673C360213}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{B0D2C6E7-BF19-4360-9137-D951C9C7CA42}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{b855b632-7dfb-4826-a172-3600ebd86948}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{BAB56818-0360-4D47-BD5F-8F0683737DA8}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{BC9E4360-F037-11CD-8701-00AA003F0F07}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{bf78a650-d489-4360-a0b7-c32471e07692}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{C6E13360-30AC-11D0-A18C-00A0C9118956}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{EDD36029-D753-4862-AA5B-5BCCAD2A4D29}\n",
      "Delete   SOFTWARE\\Classes\\CLSID\\{FB40360C-547E-4956-A3B9-D4418859BA66}\n",
      "Delete   SOFTWARE\\Classes\\DirectShow\\MediaObjects\\87fc0268-9a55-4360-95aa-004a1d9de26c\n",
      "Delete   SOFTWARE\\Classes\\DirectShow\\MediaObjects\\Categories\\f3602b3f-0592-48df-a4cd-674721e7ebeb\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{02E84540-F4A1-589F-9360-A0502E6DC9C0}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{03606A5E-2AA9-4AB6-AF8B-A562B63A8992}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{079863B7-6D47-4105-8BFE-0CDCB360E67D}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{0D296515-AD19-3602-B415-D8EC77066081}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{113AD4DC-9DA3-4197-A012-A90E360AA089}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{16083582-9360-4758-8978-46970AE14999}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{17156360-2F1A-384A-BC52-FDE93C215C5B}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{193D0DB0-2A68-5F8E-A366-E6624A36003E}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2014EFFB-5CD8-4f08-A314-0D360DA59F14}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{20160302-6989-4002-80CF-3607F377D40C}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{21c36063-738c-474a-a9d9-8dc94c4ae2bf}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{22DA703A-C764-58CB-9185-CCFAC360025A}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2360c714-4bf1-4b26-ba65-9b21316127eb}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{26036036-4010-4578-8091-0db9a1edf9c3}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{26DC9579-4C4F-4F30-BC89-649F60E36055}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2752364A-924F-3603-8F6F-6586DF98B292}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2d5d0fda-9a4a-4155-9371-8d360401b26d}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2E084360-A93A-4CFE-B892-83F4890EC8A7}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2E18EAC3-3253-4BF0-BE9F-38FEB4360C34}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2E9A5206-2360-49DF-9D9B-1762B4BEAE77}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{2F0666C6-12F7-4360-B511-A394A0553725}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{30063603-98F6-43B6-AE5B-D583968FDA4C}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{3050F360-98B5-11CF-BB82-00AA00BDCE0B}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{33F288A6-5C2C-4D27-BAC7-7536088F1219}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{34E64FB9-6B31-4D32-8B27-D286C0C33606}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{352A7616-8120-4357-BF3E-3120360A54A5}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{35A8F3AC-FE28-360F-A0C0-9A4D50C4682A}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{35BD3360-1B35-4927-BAE4-B10E70D99EFF}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{360030AF-7930-47A7-A1DD-184464BE39DE}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{36027933-A18E-4DB5-A697-F58FA7364E44}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{3609D27B-6504-4C90-9C5E-8B2F56FCD569}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{360DEFB9-A9D3-4984-A4ED-9EC734604921}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{39ccc360-e53a-4227-b72e-2dc741a6e671}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{3A704C3D-32F2-5ED8-96FE-B5F870360F6E}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{3dc36085-5fec-541b-96cf-627b2ad80d36}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{404FAFDD-1E3F-3602-BFF6-755C00613ED8}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{4169A239-F6CB-4924-99A4-CFADA3608308}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{43604B50-9EEC-55EE-8E6F-1B544DA20095}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{486360F5-6213-322B-BEFB-45221579D4AF}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{49A4B96A-169E-4D7F-8070-91AC8360FF5E}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{4A1865AB-CE88-422F-A82C-B4C360C516CC}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{4adac360-6479-568d-9cdf-10285d6d4792}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{4CF71E34-99AB-40A6-8F95-C264A360FB2C}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{561D1DDA-C42F-4B2D-8512-3ABB443604A4}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{568B3205-1D4C-4C0D-8CEA-9A360F67F107}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{58d36055-0241-555d-af7b-9f05e5daa412}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{5A773E24-D968-535E-969A-76CE3602A637}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{60695e7b-0dde-44fd-ba98-63606218db97}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{60AAA1E2-9CF9-4FDD-B24F-1B8988AB3603}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{66413A78-AB7A-4ADA-8ECE-936014B2EAD7}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{6D5174EC-F425-4CD9-8643-CF36042987F1}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{6E122360-FF65-44AE-9E45-857FE4180681}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{6E96AA70-9FFB-399D-96BF-A68436095C54}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{6fa06099-0fa1-561d-a775-1360815cd2fc}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{7133FCE1-1769-4FF9-A7C1-38D2C4D42360}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{7278e7f2-0360-5571-8c3e-ea032767c473}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{728ab360-217d-11da-b2a4-000e7bbb2b09}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{72919787-4DAB-46e3-B4C9-D8E1A2636062}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{736038D5-C833-4E50-BA25-C97521AA17A2}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{73E42360-10FC-513C-9C38-D7FB007ABBC4}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{7A460360-DA21-4B09-A8A0-8A69E728D893}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{7F2BE360-2A95-4574-A16E-B8EC025083CE}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{81926775-B981-4479-988F-DA171D627360}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{8262c7ef-b360-40be-949b-2ff42ff35693}\n",
      "Delete   SOFTWARE\\Classes\\Interface\\{8282360f-f38c-489a-9804-659612edc3b0}\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSubKeys\u001b[1;34m(Ob_S_key)\u001b[0m\n\u001b[0;32m     11\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m             \u001b[0mName\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwinreg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mEnumKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_S_key\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m             \u001b[0mL_SubKeys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mName\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mOSError\u001b[0m: [WinError 259] 没有可用的数据了。",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-87-ca33408684dd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[1;31m# 根据关键字搜索注册表对应根键的所有子健,不区分大小写。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mSearch_Keyname\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mL_Search_Keyname\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m     \u001b[0mSearch_KN_SubKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_M_MasterKey0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_M_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSearch_Keyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m     \u001b[0mSearch_KN_SubKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_M_MasterKey1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_M_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSearch_Keyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSearch_KN_SubKey\u001b[1;34m(Ob_MasterKey, Str_SubKey, Keyname)\u001b[0m\n\u001b[0;32m     48\u001b[0m     \u001b[0mOb_SKS_Key\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwinreg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpenKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SubKey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     49\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mStr_SKS_SubKey\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mSubKeys\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_SKS_Key\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 50\u001b[1;33m         \u001b[0mSearchSubkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SKS_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mKeyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     51\u001b[0m         \u001b[1;31m# print Str_M_SubKey\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSearchSubkey\u001b[1;34m(Ob_MasterKey, Str_SubKey, Keyname)\u001b[0m\n\u001b[0;32m     39\u001b[0m                 \u001b[0mDel_SubKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     40\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 41\u001b[1;33m                 \u001b[0mSearchSubkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mKeyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     42\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mWindowsError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     43\u001b[0m         \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSearchSubkey\u001b[1;34m(Ob_MasterKey, Str_SubKey, Keyname)\u001b[0m\n\u001b[0;32m     39\u001b[0m                 \u001b[0mDel_SubKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     40\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 41\u001b[1;33m                 \u001b[0mSearchSubkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mKeyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     42\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mWindowsError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     43\u001b[0m         \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSearchSubkey\u001b[1;34m(Ob_MasterKey, Str_SubKey, Keyname)\u001b[0m\n\u001b[0;32m     39\u001b[0m                 \u001b[0mDel_SubKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     40\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 41\u001b[1;33m                 \u001b[0mSearchSubkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mKeyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     42\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mWindowsError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     43\u001b[0m         \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSearchSubkey\u001b[1;34m(Ob_MasterKey, Str_SubKey, Keyname)\u001b[0m\n\u001b[0;32m     39\u001b[0m                 \u001b[0mDel_SubKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     40\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 41\u001b[1;33m                 \u001b[0mSearchSubkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SS_SubKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mKeyname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     42\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mWindowsError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     43\u001b[0m         \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSearchSubkey\u001b[1;34m(Ob_MasterKey, Str_SubKey, Keyname)\u001b[0m\n\u001b[0;32m     33\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m         \u001b[0mOb_SS_Key\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwinreg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpenKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_MasterKey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mStr_SubKey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 35\u001b[1;33m         \u001b[1;32mfor\u001b[0m \u001b[0msk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mSubKeys\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_SS_Key\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     36\u001b[0m             \u001b[0mStr_SS_SubKey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mStr_SubKey\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;34m\"\\\\\"\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0msk\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     37\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mKeyname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mIGNORECASE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-85-8ec8f546b2f9>\u001b[0m in \u001b[0;36mSubKeys\u001b[1;34m(Ob_S_key)\u001b[0m\n\u001b[0;32m     10\u001b[0m         \u001b[0mL_SubKeys\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m             \u001b[0mName\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwinreg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mEnumKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mOb_S_key\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m             \u001b[0mL_SubKeys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mName\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m             \u001b[0mi\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# 通过_winreg模块获取根键对象HKEY_LOCAL_MACHINE和HKEY_USERS，在windows系统中只有这两个根键被保存在硬盘中，其他根键要么是这两个根键的子健,要么就是volatile hive，当系统启动时在内存中创建。\n",
    "Ob_M_MasterKey0 = winreg.HKEY_LOCAL_MACHINE\n",
    "Ob_M_MasterKey1 = winreg.HKEY_USERS\n",
    "\n",
    "\n",
    "# 设置子健字符串为空，这样可以检索根键对象HKEY_LOCAL_MACHINE和HKEY_USERS下的所有子健\n",
    "Str_M_SubKey = \"\"\n",
    "\n",
    "\n",
    "# 设置检索子健的关键字，可以在list中追加多个关键字\n",
    "L_Search_Keyname = ['360']\n",
    "\n",
    "\n",
    "# 根据关键字搜索注册表对应根键的所有子健,不区分大小写。\n",
    "for Search_Keyname in L_Search_Keyname:\n",
    "    Search_KN_SubKey(Ob_M_MasterKey0, Str_M_SubKey, Search_Keyname)\n",
    "    Search_KN_SubKey(Ob_M_MasterKey1, Str_M_SubKey, Search_Keyname)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "262px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "144px",
    "left": "1017.36px",
    "right": "20px",
    "top": "118px",
    "width": "351px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
